package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dao.SysDictType;
import com.ruoyi.system.domain.query.SysDictTypeQo;
import com.ruoyi.system.domain.view.SysDictTypeVo;
import com.ruoyi.system.domain.wrapper.query.SysDictTypeWrapperQ;
import com.ruoyi.system.domain.wrapper.view.SysDictTypeWrapperV;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 字典类型分页查询
     */
    @ApiOperation(value="字典类型分页查询",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:type:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictTypeVo> list(SysDictTypeQo sysDictType)
    {
        IPage<SysDictType> page=dictTypeService.page(sysDictType.getPage(), SysDictTypeWrapperQ.getQueryWrapper(sysDictType));
        return SysDictTypeWrapperV.build().pageVO(page);
    }

    /**
     * 字典类型导出
     */
    @ApiOperation("字典类型导出")
    @HasPermi("system:type:list")
    @Log(title = "字典类型导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeQo sysDictType)
    {
        List<SysDictType> list = dictTypeService.list(SysDictTypeWrapperQ.getQueryWrapper(sysDictType));
        List<SysDictTypeVo> voList = SysDictTypeWrapperV.build().listVO(list);
        ExcelUtil<SysDictTypeVo> util = new ExcelUtil<>(SysDictTypeVo.class);
        util.exportExcel(response, voList, "字典类型数据");
    }


    /**
     * 获取字典类型详细信息
     */
    @ApiOperation("字典类型信息")
    @HasPermi("system:type:query")
    @GetMapping(value = "/get/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") String dictId)
    {
        SysDictType entity=dictTypeService.getById(dictId);
        return AjaxResult.success(SysDictTypeWrapperV.build().entityVO(entity));
    }

    /**
     * 新增字典类型
     */
    @ApiOperation("字典类型新增")
    @HasPermi("system:type:edit")
    @Log(title = "新增字典类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysDictType sysDictType)
    {
        return toAjax(dictTypeService.insert(sysDictType));
    }

    /**
     * 修改字典类型
     */
    @ApiOperation("字典类型修改")
    @HasPermi("system:type:edit")
    @Log(title = "修改字典类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysDictType sysDictType)
    {
        return toAjax(dictTypeService.updateById(sysDictType));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("字典类型删除")
    @HasPermi("system:type:edit")
    @Log(title = "删除字典类型", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{dictIds}")
    public AjaxResult remove(@PathVariable String[] dictIds)
    {
        return toAjax(dictTypeService.removeByIds(Arrays.asList(dictIds)));
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.list(null);
        return AjaxResult.success(dictTypes);
    }
}
